﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

using PowerCreator.LMS.CMS.Entity;
using PowerCreator.LMS.Web.CMS;
using PowerCreator.LMS.Common.Script;
public partial class sysadmin_NewManager_ManagerNewType : PowerCreator.LMS.Web.Base.BasePage
{
    NewType objNewType = new NewType();
    New objNew = new New();
    private PowerCreator.LMS.Web.DL.User.LoginEntity login = new PowerCreator.LMS.Web.DL.User.LoginEntity();
    private string url = "../login.aspx";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            InitPage();
            string search = GetSearch();
            BinderPager(search);
            BinderPG(search, 1);
            if (CheckUser())
            {
                btnAddNew.Enabled = true;
            }
            else
            {
                btnAddNew.Enabled = false;
            }
        }
    }


    private bool CheckUser()
    {
        bool rt = false;
        if (Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO] == null)
        {
            Script.RedirectNewUrl(url);
        }
        else
        {
            login = (PowerCreator.LMS.Web.DL.User.LoginEntity)Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO];
            if (login == null)
            {
                Script.RedirectNewUrl(url);
            }
            else
            {
                if (login.VOAdvancedUser == null)
                {
                    Script.RedirectNewUrl(url);
                }
                else if (login.VOAdvancedUser.AUserGroup == 1 || login.VOAdvancedUser.AUserGroup == 2 || login.VOAdvancedUser.AUserGroup == 3)
                {
                    rt = true;
                }
            }
        }

        return rt;

    }

    protected override void InitPage()
    {
        lbl_InfoTitle_ManagerNewType.Text = GetLanageValueByKey("lab_ManagerNewType_InfoTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_ManagerNewType.Text = GetLanageValueByKey("lab_ManagerNewType_NewsTypeList", PowerCreator.LMS.Language.LanagetItemType.Page);

        btnAddNew.Text = GetLanageValueByKey("btn_AddNew", PowerCreator.LMS.Language.LanagetItemType.Page);
        btnSearch.Text = GetLanageValueByKey("btn_Search", PowerCreator.LMS.Language.LanagetItemType.Page);
        
        base.InitPage();

        //btnAddNew.Visible = false;
        //if (IsLicensed(PowerCreator.LMS.Action.Core.ActionInfo.AddJob))
        //    btnAddNew.Visible = true;

    }
    private string GetSearch()
    {
        string search = string.Format(" NewTypeName like '%{0}%'", txtNewTypeName.Text.Trim());
        return search;
    }

    protected void BinderPager(string search)
    {
        AspNetPager1.RecordCount = objNewType.Count(search);
        AspNetPager1.PageSize = LMSPageSize;
    }
    protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
        {
            if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
            {
                LinkButton lnkbtn_editor = (LinkButton)e.Item.FindControl("lnkbtn_editor");
                LinkButton lbtn = (LinkButton)e.Item.FindControl("lnkbtn_del");
                if (CheckUser())
                {
                    lnkbtn_editor.Enabled = true;
                    lbtn.Enabled = true;
                    lbtn.Attributes.Add("onClick", "javascript:return confirm('确定删除此记录？');");
                }
                else
                {
                    lnkbtn_editor.Enabled = false;
                    lbtn.Enabled = false;
                }
            }
        }
    }

    private void BinderPG(string search,int pageIndex)
    {
        DataTable dt = objNewType.Query(search, LMSPageSize, pageIndex);
        rptNewType.DataSource = dt;
        rptNewType.DataBind();

        PreAspNetPager(AspNetPager1);
    }  

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        string search = GetSearch();
        BinderPager(search);
        BinderPG(search,1);
    }
    protected void btnAddNew_Click(object sender, EventArgs e)
    {
        string url = "AddNewType.aspx";
        Script.RedirectURL(url);
    }
  
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        string search = GetSearch();
        BinderPG(search, AspNetPager1.CurrentPageIndex);
    }
    protected void rptNewType_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        Label lbl_msg = (Label)e.Item.FindControl("lbl_msg");
        if (e.CommandName.ToLower() == "leditor")
        {
            //if (IsLicensed("NewsTypeUpdate"))
            //{
                string url = "AddNewType.aspx?NewTypeID=" + e.CommandArgument.ToString();
                Script.RedirectURL(url);
            //}
            //else
            //{
            //    lbl_msg.Text = GetLanageValueByKey("Msg_ManagerNewType_checkMsg1", PowerCreator.LMS.Language.LanagetItemType.Page);
            //}
        }
        else if (e.CommandName.ToLower() == "ldel")
        {
            //if (IsLicensed("NewsTypeDel"))
            //{
                DataTable dt_New = objNew.Query(" FKNewNewTypeID=" + e.CommandArgument.ToString());
                if (dt_New.Rows.Count != 0)
                {
                    Script.Alert(this, "", GetLanageValueByKey("Msg_ManagerNewType_checkMsg2", PowerCreator.LMS.Language.LanagetItemType.Page));
                    return;
                }
                if (objNewType.Del(int.Parse(e.CommandArgument.ToString())))
                {
                    string search = GetSearch();
                    BinderPager(search);
                    BinderPG(search, AspNetPager1.CurrentPageIndex);
                }
                else
                {
                    lbl_msg.Text = GetLanageValueByKey("Msg_ManagerQuestion_WorkNo", PowerCreator.LMS.Language.LanagetItemType.Page);
                }
            //}
            //else
            //{
            //    lbl_msg.Text = GetLanageValueByKey("Msg_ManagerNewType_checkMsg3", PowerCreator.LMS.Language.LanagetItemType.Page);
            //}
        }

    }

}
